package com.cy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.Constant;
import com.cy.entity.DiscussPost;
import com.cy.entity.User;
import com.cy.service.DiscussPostService;
import com.cy.service.LikeService;
import com.cy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: cy
 * @Date: 2021/11/08/21:10
 * @Description:
 */

//社区主页

@RequestMapping("/home")
@Controller
public class HomeController {
    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private UserService userService;

    @Resource
    private LikeService likeService;

    @GetMapping(path = "index")
    public String getIndexPage(Model model, Integer current, Integer size,
                               @RequestParam(name = "orderMode", defaultValue = "0") int orderMode){

        Page<DiscussPost> list = discussPostService.findByPage(current, size, orderMode);

//        model.addAttribute("path", "/index?orderMode="+ orderMode);


        List<Map<String, Object>> discussPosts = new ArrayList<>();
        list.getRecords().forEach(discussPost -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("post", discussPost);
            User user = userService.findUserById(discussPost.getUserId());
            map.put("user", user);

            //获取点赞数量
            long likeCount = likeService.findEntityLikeCount(Constant.ENTITY_TYPE_POST, discussPost.getId());
            map.put("likeCount", likeCount);

            discussPosts.add(map);
        });
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("page", list);
        model.addAttribute("path", "/home/index?orderMode="+orderMode);
        model.addAttribute("orderMode", orderMode);
        return "/index";
    }


    @GetMapping("/findByPage")
    public String findByPage(){
        Page<DiscussPost> discussPostPage = discussPostService.findByPage(1, 10,0);
        List<Map<String, Object>> list = new ArrayList<>();
        if(list != null){
            for (DiscussPost discussPost : discussPostPage.getRecords()) {
                Map<String, Object> map = new HashMap<>();
                map.put("discussPost", discussPost);
                User user = userService.findUserById(discussPost.getUserId());
                map.put("user", user);
                list.add(map);
            }

        }
        return null;
    }

    @GetMapping("/error")
    public String error(){
        return "/error/500";
    }


    @GetMapping(path = "/denied")
    public String denied(){
        return "/error/404";
    }

}
